package com.android.kotlincoroutinemvvmdemo.dialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ActivityInfo.WindowLayout
import android.content.res.Configuration
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import com.android.kotlincoroutinemvvmdemo.Constants
import com.android.kotlincoroutinemvvmdemo.CoroutineMVVMApp

//从onCreateView中返回view,通常用于创建复杂内容弹窗或全屏展示效果的场景,UI复杂,功能复杂,一般有网络请求等异步操作.
abstract class BaseDialogFragment2<T : ViewDataBinding> : DialogFragment() {

    protected lateinit var mBinding : T

    private var mReceiver : AdapterUIModeNightBroadcastReceiver? = null

    private val mIntentFilter by lazy {
        IntentFilter().apply {
            addAction(Constants.DAY_NIGHT_UI_MODE_CHANGE)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, 0)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding =
            DataBindingUtil.inflate(inflater, getLayoutId(), container, false)
        //设置不能取消
        //dialog?.apply {
        //    setCancelable(false)
        //    setCanceledOnTouchOutside(false)
        //}
        dialog?.window?.apply {
            setBackgroundDrawableResource(android.R.color.transparent)
            decorView.setPadding(0, 0, 0, 0)
            val mAttribute = attributes
            mAttribute.gravity = Gravity.CENTER
            //通过WindowManager.LayoutParams设置宽高
            mAttribute.width = WindowManager.LayoutParams.WRAP_CONTENT
            mAttribute.height = WindowManager.LayoutParams.WRAP_CONTENT
            attributes = mAttribute
        }
        init(savedInstanceState)
        return mBinding.root
    }

    override fun onStart() {
        super.onStart()
        mReceiver = AdapterUIModeNightBroadcastReceiver()
        requireContext().registerReceiver(mReceiver, mIntentFilter)
        adapterUIModeNightForDialog(CoroutineMVVMApp.instance!!.isDarkTheme())
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        //用来处理屏幕旋转
    }

    override fun onStop() {
        super.onStop()
        requireContext().unregisterReceiver(mReceiver)
        mReceiver = null
    }

    abstract fun getLayoutId(): Int

    abstract fun init(savedInstanceState: Bundle?)

    open fun adapterUIModeNightForDialog(uiModeNight: Boolean) {}

    inner class AdapterUIModeNightBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (context == null || intent == null) {
                return
            }
            when (intent.action) {
                Constants.DAY_NIGHT_UI_MODE_CHANGE -> {
                    val dayNightYes = intent.getBooleanExtra(Constants.DAY_NIGHT_FLAG, false)
                    adapterUIModeNightForDialog(dayNightYes)
                }
            }
        }
    }

}